package com.my.blog.myblog.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.blog.myblog.entity.Visit;
import com.my.blog.myblog.entity.custom.TimeCount;
import com.my.blog.myblog.service.VisitService;
import com.my.blog.utils.ResultBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.bind.ValidationEventLocator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/3 16:08
 * @Description：
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminVisitController {

    @Resource
    private VisitService visitService;

    //查询访客记录并根据ip分组+模糊
    @PostMapping("selectLikeVisitListGroupByIp")
    public Map<String,Object> selectLikeVisitListGroupByIp(Visit visit,
                                         @RequestParam(value = "page",defaultValue = "1")Integer page,
                                         @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                         @RequestParam(value = "startTime")String startTime,
                                         @RequestParam(value = "endTime")String endTime){
        QueryWrapper<Visit> queryWrapper=new QueryWrapper<>();
        Map<String,Object> conditionMap = new HashMap<>();
        if(StrUtil.isNotEmpty(startTime)){
            conditionMap.put("startTime",startTime);
        }
        if(StrUtil.isNotEmpty(endTime)){
            conditionMap.put("endTime",endTime);
        }
        if(StrUtil.isNotEmpty(visit.getIp())){
            conditionMap.put("ip",visit.getIp());
        }
        if(StrUtil.isNotEmpty(visit.getBrowserType())){
            conditionMap.put("browserType",visit.getBrowserType());
        }
        if(StrUtil.isNotEmpty(visit.getPlatformType())){
            conditionMap.put("platformType",visit.getPlatformType());
        }
        if(StrUtil.isNotEmpty(visit.getCity())){
            conditionMap.put("city",visit.getCity());
        }
        //开始分页
        PageHelper.startPage(page,pageSize);

        //总数据
        List<?> visitList = visitService.selectLikeVisitListGroupByIp(conditionMap);

        //分好页了
        PageInfo pageInfo=new PageInfo(visitList);

        Map<String,Object> map=new HashMap<>();
        map.put("visitList",visitList);
        map.put("pageInfo",pageInfo);
        if(ObjectUtil.isNotEmpty(visitList)){
            map.put("status",200);
        }else {
            map.put("status",500);
        }
        return map;
    }


    //查询访客记录+模糊
    @PostMapping("/selectLikeVisitListByPage")
    public ResultBean<IPage<Visit>> selectLikeVisitListByPage(Visit visit,
                                          @RequestParam(value = "page",defaultValue = "1")Integer page,
                                          @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                          @RequestParam(value = "startTime")String startTime,
                                          @RequestParam(value = "endTime")String endTime){
        ResultBean<IPage<Visit>> resultBean=new ResultBean<>();
        QueryWrapper<Visit> queryWrapper=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(startTime)){
            queryWrapper.lambda().ge(Visit::getTime,startTime);
        }
        if(StrUtil.isNotEmpty(endTime)){
            queryWrapper.lambda().le(Visit::getTime,endTime);
        }
        if(StrUtil.isNotEmpty(visit.getIp())){
            queryWrapper.lambda().like(Visit::getIp,visit.getIp());
        }
        if(StrUtil.isNotEmpty(visit.getBrowserType())){
            queryWrapper.lambda().or().like(Visit::getBrowserType,visit.getBrowserType());
        }
        if(StrUtil.isNotEmpty(visit.getPlatformType())){
            queryWrapper.lambda().or().like(Visit::getPlatformType,visit.getPlatformType());
        }
        if(StrUtil.isNotEmpty(visit.getCity())){
            queryWrapper.lambda().or().like(Visit::getCity,visit.getCity());
        }
        queryWrapper.lambda().orderByDesc(Visit::getTime);
        Page<Visit> visitPage = visitService.page(new Page<>(page, pageSize), queryWrapper);
        if(ObjectUtil.isNotEmpty(visitPage.getRecords())){
            resultBean.setData(visitPage);
        }else {
            visitPage.setTotal(0);
            resultBean.setData(visitPage);
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    //查询登陆ip总数及总访问次数
    @PostMapping("selectVisitListByIp")
    public Map<String,Object> selectVisitListGroupByIp() {
        QueryWrapper<Visit> queryWrapper = new QueryWrapper<>();
        //将相同ip和浏览器信息分组，相当于去重看有那些用户
        queryWrapper.lambda().groupBy(Visit::getIp, Visit::getUserAgent).select(Visit::getIp, Visit::getUserAgent);
        Page<Visit> page = visitService.page(new Page<>(), queryWrapper);
        Map<String,Object> map=new HashMap<>();
        //有几个用户
        map.put("user",page.getTotal());
        //总访问记录
        map.put("count",visitService.count());
        return map;
    }


        //根据日期查询访客总数
    @GetMapping("/selectVisitListByDate")
    public ResultBean<List<TimeCount>> selectVisitListByDate(@RequestParam(value = "startTime")String startTime,
                                                             @RequestParam(value = "endTime")String endTime,
                                                             @RequestParam(value = "format")String format){

        //System.out.println("进来了0.0");
        Map<String,Object> map=new HashMap<>();
        //根据年月日不同日期格式的查询，赋format不同的值，具体到某年或月或日
        if("day".equals(format)){
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("format","day");
        }
        if("month".equals(format)){
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("format","month");
        }
        if("year".equals(format)){
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("format","year");
        }
        List<TimeCount> timeCounts = visitService.selectVisitListByDate(map);
        ResultBean<List<TimeCount>> resultBean=new ResultBean<>();
        resultBean.setCode(ResultBean.SUCCESS);
        resultBean.setData(timeCounts);
        //System.out.println(timeCounts);
        return resultBean;
    }
}
